if(!window.mc){
	window.mc = function(){};
}
mc.types = function(){};
mc.types.i = function(){};
mc.types.v = function(){};

mc.types.draw_value = function(desc,value){
	return this.v[desc.type].apply(this,arguments);
}
mc.types.draw_input = function(desc,name,value,force){
	return this.i[desc.type].apply(this,arguments);
}
mc.types.v.text = function(desc,value){
	var out = '';
	if(value != undefined){
		out += value;
	}
	return out;
}
mc.types.i.text = function(desc,name,value,force){
	var out = '<textarea name="' + name + '"';
	if(desc.attr){
		for(var a in desc.attr){
			out += ' ' + a + '="' + desc.attr[a] + '"';
		}
	}
	out += '>';
	if(value != undefined){
		out += value.replace("&","&amp;").replace("<","&lt;").replace(">","&gt;");
	}
	out += '</textarea>';
	return out;
}
mc.types.v.string = function(desc,value){
	var out = '';
	if(value != undefined){
		out += value;
	}
	return out;
}
mc.types.i.string = function(desc,name,value,force){
	var out = '<input type="text" name="' + name + '"';
	if(value != undefined){
		out += ' value="' + value + '"';
	}
	if(desc.size){
		out += ' size="' + desc.size + '"';
	}
	if(desc.maxlen){
		out += ' maxlength="' + desc.maxlen + '"';
	}
	out += '>';
	return out;
}
mc.types.v.checkbox = function(desc,value){
	var out = '';
	if(value && value != "0"){
		out += desc.value;
	}
	return out;
}
mc.types.i.checkbox = function(desc,name,value,force){
	var out = '<input type="checkbox" name="' + name + '" value="' + desc.value + '"';
	if(value && value != "0"){
		out += ' checked';
	}
	out += '>';
	return out;
}
mc.types.v.radio = function(desc,value){
	var out = '';
	for(var i = 0; i < desc.list.length; i++){
		if(value == desc.list[i].value){
			out += desc.list[i].title;
			break;
		}
	}
	return out;
}
mc.types.i.radio = function(desc,name,value,force){
	var out = '';
	var prefix = name + '_' + Math.floor(Math.random()*1000) + '_';
	for(var i = 0; i < desc.list.length; i++){
		out += '<input type="radio" name="' + name + '" value="' + desc.list[i].value + '"';
		if(value == desc.list[i].value){
			out += ' checked';
		}
		out += ' id="' + prefix + i +'"> ';
		out += '<label for="' + prefix + i +'">' + desc.list[i].title + '</label><br>';
	}
	return out;
}
mc.types.v.list = function(desc,value){
	var out = '';
	for(var i = 0; i < desc.list.length; i++){
		if(value == desc.list[i].value){
			out += desc.list[i].title;
			break;
		}
	}
	return out;
}
mc.types.i.list = function(desc,name,value,force){
	var out = '<select name="' + name + '">';
	for(var i = 0; i < desc.list.length; i++){
		out += '<option value="' + desc.list[i].value + '"';
		if(value == desc.list[i].value){
			out += ' selected';
		}
		out += '>' + desc.list[i].title + '</option>';
	}
	out += '</select>';
	return out;
}
mc.types.v.image = function(desc,value){
	if(!value){
		return "";
	}
	var out = '<img src="' + window.path_to_img + value + '"';
	if(desc.attr){
		for(var a in desc.attr){
			out += ' ' + a + '="' + desc.attr[a] + '"';
		}
	}
	out += '>';
	return out;
}
mc.types.i.image = function(desc,name,value,force){
	var out = '<input type="text" name="' + name + '"';
	if(value != undefined){
		out += ' value="' + value + '"';
	}
	if(desc.size){
		out += ' size="' + desc.size + '"';
	}
	if(desc.maxlen){
		out += ' maxlength="' + desc.maxlen + '"';
	}
	out += '>';
	return out;
}
mc.types.v.hash = function(desc,value){
	var out = '<table>';
	for(var i = 0; i < desc.attr.length; i++){
		var n = desc.attr[i].name;
		out += '<tr><td>' + desc.attr[i].title + '</td><td>';
		out += mc.types.draw_value(desc.attr[i], value ? value[n]:null);
		out += '</td></tr>';
	}
	out += '</table>';
	return out;
}
mc.types.i.hash = function(desc,name,value,force){
	var out = '<table>';
	for(var i = 0; i < desc.attr.length; i++){
		var n = desc.attr[i].name;
		out += '<tr><td><span title="' + desc.attr[i].name + '">' + desc.attr[i].title + '</span></td><td>';
		out += mc.types.draw_input(desc.attr[i], name + "." + n, value ? value[n]:null);
		out += '</td></tr>';
	}
	out += '</table>';
	return out;
}
mc.types.v.array = function(desc,value){
	var out = '<table><tr><td>№</td>';
	for(var i = 0; i < desc.attr.length; i++){
		out += '<td>' + desc.attr[i].title + '</td>';
	}
	out += '</tr>';
	if(!value || value.length == undefined){
		value = [ value ];
	}
	for(var r = 0; r < value.length; r++){
		var row = '<tr><td>' + r + '</td>';
		var crv = 0;
		for(var i = 0; i < desc.attr.length; i++){
			var n = desc.attr[i].name;
			var rv = value[r] ? value[r][n] : null;
			row += '<td>';
			row += mc.types.draw_value(desc.attr[i], rv)
			row += '</td>';
			if(rv){
				crv++;
			}
		}
		row += '</tr>';
		if(crv>0){
			out += row;
		}
	}
	out += '</table>';
	return out;
}
mc.types.i.array = function(desc,name,value,force){
	var out = '<table><thead><tr><td>№</td>';
	for(var i = 0; i < desc.attr.length; i++){
		out += '<td><span title="' + desc.attr[i].name + '">' + desc.attr[i].title + '</span></td>';
	}
	out += '</tr>';
	if(!value || value.length == undefined){
		value = [ value ];
	}
	out += '</thead><tbody>';
	for(var r = 0; r < value.length; r++){
		var row = '<tr><td><a href="#" onclick="return mc.types.delete_row(this)">' + r + '</a></td>';
		var crv = 0;
		for(var i = 0; i < desc.attr.length; i++){
			var n = desc.attr[i].name;
			var rv = value[r] ? value[r][n] : null;
			row += '<td>';
			row += mc.types.draw_input(desc.attr[i],name +'.' + n, rv)
			row += '</td>';
			if(rv){
				crv++;
			}
		}
		row += '</tr>';
		if(crv>0){
			out += row;
		}
	}
	out += '</tbody>';

	var row = '<td><a href="#" onclick="return mc.types.delete_row(this)">-</a></td>';
	for(var i = 0; i < desc.attr.length; i++){
		var n = desc.attr[i].name;
		row += '<td>';
		row += mc.types.draw_input(desc.attr[i],name +'.' + n)
		row += '</td>';
	}
	row += '';
	var row2 = escape(row);//.replace(/"/g,"\\"+'"').replace(/<\r?\n/g,"").replace(/</g,"&lt;").replace(/>/g,"&gt;");
	//alert(row + "\n" + row2);

	out += '<tfoot><tr><td>&nbsp;</td><td colspan=' + desc.attr.length + '>';

	out += '<a href="#" onclick="return mc.types.append_row(this)" val="'+row2+'">+</a>';

	out += '</td></tr></tfoot>';
	out += '</table>';
	return out;
}
mc.types.append_row = function(obj){
	var o = $(obj);
	var r = o.attr("val");
	var p = o.parents("table:eq(0)");
	p.find("tbody").append($("<tr/>").append(unescape(r)));
	return false;
}
mc.types.delete_row = function(obj){
	var o = $(obj);
	var r = o.attr("val");
	var p = o.parents("tr:eq(0)");
	p.remove();
	return false;
}
